package com.weepal.pavement.mqtt.demo.util;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

public class test {
    public static String hex2dec(String x, int type) {
        String v;
        switch (type) {
            case 1:
                v = wufuhao.hex2dec(x);
                break;
            case 2:
                v = youfuhao.hex2dec(x);
                break;

            case 3:
                v = highlow.hex2dec(x);
                break;

            case 4:
                v = lowhigh.hex2dec(x);
                break;
            default:
                v = null;
        }
        return v;
    }

    /**
     * 无符号位16进制转10进制
     */
    private static class wufuhao {
        public static String hex2dec(String x) {
            if (x == null || x.equals("")) {
                return null;
            }
            return String.valueOf(Long.parseLong(x, 16));
        }
    }

    /**
     * 有符号位16进制转10进制
     */
    private static class youfuhao {
        public static String hex2dec(String x) {
            if (x == null || x.equals("")) {
                return null;
            }
            String binary = binary(hexStringTobyte(x), 2);
            return change(binary);
        }
    }

    private static class highlow {
        public static String hex2dec(String x) {
            if (x == null || x.equals("")) {
                return null;
            }
            if (x.length() % 2 != 0) {
                return null;
            }
            int midIndex = x.length() / 2;
            String high = x.substring(0, midIndex);
            String low = x.substring(midIndex);
            int lowv = Integer.parseInt(low, 16);
            String highBinary = binary(hexStringTobyte(high), 2);
            String highv = change(highBinary);
            return highv + "." + lowv;
        }
    }

    private static class lowhigh {
        public static String hex2dec(String x) {
            if (x == null || x.equals("")) {
                return null;
            }
            if (x.length() % 2 != 0) {
                return null;
            }
            int midIndex = x.length() / 2;
            String low = x.substring(0, midIndex);
            String high = x.substring(midIndex);
            int lowv = Integer.parseInt(low, 16);
            String highBinary = binary(hexStringTobyte(high), 2);
            String highv = change(highBinary);
            return highv + "." + lowv;
        }
    }


    //16进制转10进制byte数组
    private static byte[] hexStringTobyte(String s) {
        s = s.trim();
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(
                        s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return baKeyword;
    }

    //10进制转2进制
    private static String binary(byte[] bytes, int radix) {
        return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
    }

    private static String change(String tem) {
        final int MAX_LENGTH = 16;
        //负数，补码转源码后转10进制
        if (tem.length() == MAX_LENGTH && tem.substring(0, 1).equals("1")) {

            //拆分为数组
            String[] temArray = new String[tem.length()];
            for (int i = 0; i < tem.length(); i++) {
                temArray[i] = tem.substring(i, i + 1);
            }
            //取反
            for (int i = 0; i < temArray.length; i++) {


                if (temArray[i].equals("1")) {
                    temArray[i] = "0";
                } else {
                    temArray[i] = "1";
                }

                if (i == 0) {
                    tem = temArray[i];
                } else {
                    tem += temArray[i];
                }
            }
            //末位+1
            tem = addBinary(tem, "1");
            //2进制转10进制
            BigInteger bi = new BigInteger(tem, 2);
            tem = "-" + String.valueOf(bi);
        }
        //正数
        else {
            //2进制转10进制
            BigInteger bi = new BigInteger(tem, 2);
            tem = String.valueOf(bi);
        }

        return tem;
    }

    private static String addBinary(String a, String b) {
        int carry = 0; // 保存进位
        int sum = 0; // 存储进位与两个位之间的和
        int opa = 0;
        int opb = 0;
        StringBuilder res = new StringBuilder();
        //将两个二进制数位补齐，在短的前面添0
        while (a.length() != b.length()) {
            if (a.length() > b.length()) {
                b = "0" + b;
            } else {
                a = "0" + a;
            }
        }
        //从后往前做加法
        for (int i = a.length() - 1; i >= 0; i--) {
            opa = a.charAt(i) - '0'; //String a 中的字符的ASCII码值与‘0’字符的差值
            opb = b.charAt(i) - '0';       //String b 中的字符的ASCII码值与‘0’字符的差值
            sum = opa + opb + carry;
            if (sum >= 2) {
                carry = 1;
                res.append((sum - 2));
            } else {
                carry = 0;
                res.append(sum);
            }
        }
        if (carry == 1) {
            res.append("1");
        }
        return res.reverse().toString();
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "f0c1";
        String binary = binary(hexStringTobyte(s), 2);
        System.out.println(binary);
//        String foo = foo(binary);
//        System.out.println(foo);
        String s2 = "12345678";
        int i = s2.length() / 2;
        String high = s2.substring(0, i);
        String low = s2.substring(i);
        System.out.println(high + "///" + low);
    }

}
